package com.elite.scheduler.jobs;

import com.elite.scheduler.dao.OrderAutoDao;
import com.elite.scheduler.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务类 Created by Ay on 2018/5/7.
 */
@Slf4j
@DisallowConcurrentExecution
public class ObjectSchedulerJob extends QuartzJobBean {
    public static final String STRING_RPTID = "rptId";
    public static final String STRING_DBPOOL = "dbpool";

    @Override
    protected void executeInternal(JobExecutionContext context) {
        String timer_guid = null;
        try {
            JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
            Map<String, Object> schedulerTemp = (Map<String, Object>) jobDataMap.get("schedulerTemp");
            String type = (String) jobDataMap.get("type");
            log.debug("定时任务执行一次, 定时任务详情: {}", schedulerTemp);
            if (StringUtil.isNotNull(type) && "reporter".equalsIgnoreCase(type)) {
                executeReportV1(schedulerTemp);
            } else {
                SchedulerTypeJob schedulerTypeJob = (SchedulerTypeJob) SpringContextUtil.getBean("schedulerTypeJob");
                String sendtype = StringUtil.parseNull(schedulerTemp.get("sendtype"));
                String epid = (String) jobDataMap.get("epid");
                log.debug("execute " + sendtype + " task ----:" + CommonUtil.getDate());

                if (schedulerTypeJob != null) {
                    if ("EMAIL".equalsIgnoreCase(sendtype)) {
                        schedulerTypeJob.EmailJob(schedulerTemp, epid);
                    } else if ("SMS".equalsIgnoreCase(sendtype)) {
                        schedulerTypeJob.sendSms(schedulerTemp, epid);
                    } else if ("CALLAPI".equalsIgnoreCase(sendtype)) {
                        schedulerTypeJob.callAPIJob(schedulerTemp);
                    } else if ("OBJECTSQL—CALLAPI".equalsIgnoreCase(sendtype)) {
                        schedulerTypeJob.objectCallAPIJob(schedulerTemp, epid);
                    } else if ("SERVERCMD".equalsIgnoreCase(sendtype)) {
                        schedulerTypeJob.callServerCmd(schedulerTemp);
                    } else if ("EXECUTE-SQL".equalsIgnoreCase(sendtype)) {
                        schedulerTypeJob.executeSql(schedulerTemp);
                    } else if ("ESLOBCLEAR".equalsIgnoreCase(sendtype)) {
                        schedulerTypeJob.ESLogClear();
                    }
                } else {
                    log.warn("ℹScheduler定时任务 初始化中...");

                }
            }

        } catch (Exception e) {
            log.error("⛔定时任务执行异常! Timer_Guid:" + timer_guid + " 错误信息:" + e.getMessage(), e);
        }
    }

    //	public void executeReport(Map<String, Object> schedulerTemp) throws Exception {
    //		org.json.JSONObject ngsTokenBody = CommonUtil.getNgsToken(0);
    //		String token = ngsTokenBody.optString("token");
    //		String digest = ngsTokenBody.optString("ordindex");
    //		Map<String, Object> map = new HashMap<>();
    //		if (StringUtil.isNotNull(token)) {
    //			String rptId = (String) schedulerTemp.get(STRING_RPTID);
    //			OrderAutoDao orderAutoDao = (OrderAutoDao) SpringContextUtil.getBean("orderAutoDao");
    //			List<Map<String, Object>> list = orderAutoDao.queryNewFlagByRptId(rptId);
    //			boolean newFlag = "1".equals(StringUtil.parseNull(list.get(0).get("NEWFLAG")));
    //			map.put("token", token);
    //			map.put("digest", digest);
    //			map.put("new", newFlag ? 1 : 0);
    //			map.put(STRING_RPTID, schedulerTemp.get(STRING_RPTID));
    //			map.put("rptName", schedulerTemp.get("rptName"));
    //			map.put(STRING_DBPOOL, StringUtil.parseNull(TimerConfigParamsUtil.dbPool));
    //			map.put("timerGuid", schedulerTemp.get("REPORTER_RELEASE_GUID"));
    //			map.put("rptGrpId", "");
    //			String reporterParams = (String) schedulerTemp.get("reporterparams");
    //			map.put("paras", ReporterUtil.getReporterParams(reporterParams));
    //			map.put("emailServicers", schedulerTemp.get("emailreceivers"));
    //			SchedulerTypeJob.reporterJob(map);
    //		} else {
    //			log.debug("获取ngsLogin token 失败！");
    //		}
    //	}

    public void executeReportV1(Map<String, Object> schedulerTemp) throws Exception {
        CommonUtil commonUtil = (CommonUtil) SpringContextUtil.getBean("commonUtil");
        SchedulerTypeJob schedulerTypeJob = (SchedulerTypeJob) SpringContextUtil.getBean("schedulerTypeJob");
        org.json.JSONObject ngsTokenBody = commonUtil.getNgsToken(0);
        String token = ngsTokenBody.optString("token");
        String digest = ngsTokenBody.optString("ordindex");
        if (StringUtil.isNotNull(token)) {
            Map<String, Object> map = new HashMap<>();
            String rptId = (String) schedulerTemp.get(STRING_RPTID);
            OrderAutoDao orderAutoDao = (OrderAutoDao) SpringContextUtil.getBean("orderAutoDao");
            List<Map<String, Object>> list = orderAutoDao.queryNewFlagByRptId(rptId);
            boolean newFlag = "1".equals(StringUtil.parseNull(list.get(0).get("NEWFLAG")));
            map.put("token", token);
            map.put("digest", digest);
            map.put("new", newFlag ? 1 : 0);
            map.put(STRING_RPTID, schedulerTemp.get(STRING_RPTID));
            map.put("rptName", schedulerTemp.get("rptName"));
            map.put(STRING_DBPOOL, StringUtil.parseNull(TimerConfigParamsUtil.dbPool));
            map.put("timerGuid", schedulerTemp.get("REPORTER_RELEASE_GUID"));
            map.put("rptGrpId", "");
            String reporterParams = (String) schedulerTemp.get("reporterparams");
            map.put("paras", ReporterUtil.getReporterParams(reporterParams));
            map.put("emailServicers", schedulerTemp.get("emailreceivers"));
            map.put("emailCcReceiver", schedulerTemp.get("emailccreceivers"));
            map.put("emailcontent", schedulerTemp.get("emailcontent"));
            schedulerTypeJob.reporterJobV1(map);
        } else {
            log.debug("获取ngsLogin token 失败！");
        }
    }
}